#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdlib.h>
#include <strings.h>

#define LINUX_PAGE_SZ 4096

int roundup_to_4096(int num) {
    return ((num + (LINUX_PAGE_SZ - 1)) / LINUX_PAGE_SZ) * LINUX_PAGE_SZ;
}

void* malloc1(size_t size)
{   
    void* p;
    if (size <= 0) {
        return NULL;
    }

    p = sbrk(0);
    if (sbrk(size) == (void *)-1) {
        return NULL;
    }

    return p;
}

#define DEBUG 0
int main(int argc, char **arg)
{
    if (argc < 1) {
        return -1;
    }

    int size = atoi(arg[1]);
    #if DEBUG
        printf("malloc %d\n", size);
    #endif

	//sleep(20);

    void *pos;
    pos = malloc1(size);
    if (!pos) {
        return 0;
    }
    #if DEBUG
        printf("%08x\n", pos);
    #endif

    // force init to alloc page, otherwise linux COW will affect
    bzero(pos, size);

    void *cur;
    cur = sbrk(0);
    #if DEBUG
        printf("%08x\n", cur);
    #endif
    printf("%d\n", (int)(cur - pos));
    return 1;
}


